In [1]:
import itertools
In [9]:
import numpy as np
Figure out how to divide an iterable into evenly-sized chunks.
In [3]:
ids = ('C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8', 'C9')
Suppose the iterable is a tuple of all possible pairs from ids:
In [4]:
pairs = tuple(itertools.combinations(ids, 2))
In [5]:
len(pairs)
Out[5]:
Also suppose that there are 16 processors, each operating on a single chunk.
In [6]:
numProc = 16
In [2]:
def get_chunk_lengths(vec, numProc):
"""Get the length of each chunk"""
numDivFloor = len(vec)//numProc
lengths = [numDivFloor]*numProc
remainder = len(vec) - numDivFloor*numProc
if remainder > len(vec):
print('Error: remainder greater than vector length')
return
else:
for i in range(remainder):
lengths[i] += 1
return lengths
In [7]:
splitlengths = get_chunk_lengths(pairs, numProc)
In [8]:
splitlengths
Out[8]:
In [16]:
divpos = np.cumsum([0] + splitlengths)
In [17]:
divpos
Out[17]:
In [18]:
len(divpos)
Out[18]:
In [22]:
def chunks(divpos, vec):
for i in range(len(divpos)-1):
yield vec[divpos[i]:divpos[i+1]]
In [23]:
tuple(chunks(divpos, pairs))
Out[23]:
Try it on an iterable that is the range of integers from 10 to 74, with number of processors equal to 7:
In [28]:
numProc = 7
v = tuple(range(10,75))
splitLengths = get_chunk_lengths(v, numProc)
divpos = np.cumsum([0] + splitLengths)
tuple(chunks(divpos, v))
Out[28]:
In [ ]: